package city.spring.modules.material.service.impl;

import city.spring.modules.material.dto.StorehouseDTO;
import city.spring.modules.material.entity.ShelfEntity;
import city.spring.modules.material.entity.ShelflevelEntity;
import city.spring.modules.material.entity.StorehouseEntity;
import city.spring.modules.material.repository.StorehouseRepository;
import city.spring.modules.material.service.ShelfService;
import city.spring.modules.material.service.ShelflevelService;
import city.spring.modules.material.service.StorehouseService;
import city.spring.modules.material.vo.StorehouseVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = {StorehouseServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class StorehouseServiceImpl extends ServiceImpl<StorehouseRepository, StorehouseEntity> implements StorehouseService {

    private final StorehouseRepository storehouseRepository;
    private final ShelfService shelfService;
    private final ShelflevelService shelflevelService;

    public StorehouseServiceImpl(StorehouseRepository storehouseRepository, ShelfService shelfService, ShelflevelService shelflevelService) {
        this.storehouseRepository = storehouseRepository;
        this.shelfService = shelfService;
        this.shelflevelService = shelflevelService;
    }

    @Async
    @Override
    public void saveStorehouse(StorehouseDTO entity) {
        // 新增出库时，默认出库为使用状态
        entity.setStatus("使用");
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }

        Integer shelfs = entity.getShelfs();
        Integer shelflevels = entity.getShelflevels();
//        Integer beds = entity.getBeds();
        if (shelfs == null || shelfs < 0) {
            return;
        }
        if (shelflevels == null || shelflevels < 0) {
            return;
        }

        // 先保存所有货架信息
        List<ShelfEntity> shelfEntities = new ArrayList<>();
        for (int shelfIndex = 0; shelfIndex < shelfs; shelfIndex++) {
            ShelfEntity shelfEntity = new ShelfEntity();
            shelfEntity.setId(IdWorker.getIdStr());
            shelfEntity.setStorehouseId(String.valueOf(entity.getId()));
            shelfEntity.setCode(String.valueOf(shelfIndex + 1));
            shelfEntity.setSname(String.format("%d号", shelfIndex + 1));
            shelfEntity.setType(entity.getType());
            shelfEntities.add(shelfEntity);
        }

        // 最后保存所有货架的层次信息
        List<ShelflevelEntity> shelflevelEntities = new ArrayList<>();
        for (ShelfEntity shelfEntity : shelfEntities) {
            for (int shelflevelIndex = 0; shelflevelIndex < shelflevels; shelflevelIndex++) {
                ShelflevelEntity shelfLevelEntity = new ShelflevelEntity();
                shelfLevelEntity.setId(IdWorker.getIdStr());
                shelfLevelEntity.setShelfId(shelfEntity.getId());
                shelfLevelEntity.setCode(String.valueOf(shelflevelIndex + 1));
                shelfLevelEntity.setSname(String.format("%d层", shelflevelIndex + 1));
                shelflevelEntities.add(shelfLevelEntity);
            }
        }
        shelfService.saveBatch(shelfEntities);
        shelflevelService.saveBatch(shelflevelEntities);
    }

    @Override
    public void updateStorehouse(StorehouseEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteStorehouse(String primaryKey) {
        // 删除仓库：修改仓库的状态为：已删除
        StorehouseEntity storehouseEntity = storehouseRepository.getById(primaryKey);
        storehouseEntity.setStatus("已删除");
        updateById(storehouseEntity);
//        removeById(primaryKey);
    }

    @Override
    public void deleteStorehouse(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public StorehouseEntity getById(String id) {
        return storehouseRepository.getById(id);
    }

    @Override
    public IPage<StorehouseVO> listPage(Page<? extends StorehouseEntity> page, Wrapper<? extends StorehouseEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public List<StorehouseEntity> getStorehouseAll() {
        return storehouseRepository.getStorehouseAll();
    }


}
